package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.init.MaintainerManufacturerInit;
import com.gable.exhibition.init.ManufacturerInit;
import com.gable.exhibition.init.ManufacturerSupplierInit;
import com.gable.exhibition.init.SupplierManufacturerInit;
import com.gable.exhibition.model.Manufacturer;
import com.gable.exhibition.model.ManufacturerSupplier;
import com.gable.exhibition.model.Supplier;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.BaseService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lesson on 2018/2/6.
 */
@Controller
@RequestMapping(value="/base")
public class BaseController {

        Logger logger = Logger.getLogger(this.getClass());

        @Autowired
        BaseService baseService;

    /**
     * 获取医院列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/queryHosital",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospital(HttpServletRequest request, HttpServletResponse response){
        try {
            User user = CacheData.getSessionUser(null,1);

            Map map = new HashMap();
            if(!StringUtil.isEmpty(request.getParameter("provinceId"))){
                map.put("provinceId",Long.parseLong(request.getParameter("provinceId")));
            }
            if(!StringUtil.isEmpty(request.getParameter("cityId"))){
                map.put("cityId",Long.parseLong(request.getParameter("cityId")));
            }
            if(!StringUtil.isEmpty(request.getParameter("hospitalId"))){
                map.put("hospitalId",user.getHospitalId());
            }
            ReturnResultUtil rru = baseService.queryHospitalBy(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e) {
            e.printStackTrace();
        }
       return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 获取厂商列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/queryManufacturer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryManufacturer(HttpServletRequest request, HttpServletResponse response){
        try {
            Map map = new HashMap();
            if(!StringUtil.isEmpty(request.getParameter("provinceId"))){
                map.put("provinceId",Long.parseLong(request.getParameter("provinceId")));
            }
            if(!StringUtil.isEmpty(request.getParameter("cityId"))){
                map.put("cityId",Long.parseLong(request.getParameter("cityId")));
            }
            ReturnResultUtil rru = baseService.queryManufacturer(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


    /**
     * 获取维修商列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/queryMaintainer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMaintainer(HttpServletRequest request, HttpServletResponse response){
        try{
            ReturnResultUtil rru = baseService.queryMaintainer();
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 获取省份列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryProvince",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryProvince(HttpServletRequest request, HttpServletResponse response){
        try{
            ReturnResultUtil rru = baseService.queryProvince();
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 获取城市列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryCity",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryCity(HttpServletRequest request, HttpServletResponse response){
        try{
            Long provinceId = null;
            if(!StringUtil.isEmpty(request.getParameter("provinceId")))
                provinceId = Long.parseLong(request.getParameter("provinceId"));

            ReturnResultUtil rru = baseService.queryCity(provinceId);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


    /**
     * 获取区域列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryArea",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryArea(HttpServletRequest request, HttpServletResponse response){
        try{
            Long provinceId = null;
            if(!StringUtil.isEmpty(request.getParameter("provinceId")))
                provinceId = Long.parseLong(request.getParameter("provinceId"));
            Long cityId = null;
            if(!StringUtil.isEmpty(request.getParameter("cityId"))){
                cityId =  Long.parseLong(request.getParameter("cityId"));
            }
            Map map = new HashMap();
            map.put("provinceId",provinceId);
            map.put("cityId",cityId);
            ReturnResultUtil rru = baseService.queryArea(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 获取字典表里面对应数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryItem",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryItem(HttpServletRequest request, HttpServletResponse response){
        try {
            if(StringUtil.isEmpty(request.getParameter("type"))){
                return new ReturnResultUtil(500, MessageUtil.ITEMTYPECANNOTEMPTY);
            }
            String type = request.getParameter("type");
            ReturnResultUtil rru = baseService.queryItem(type);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


    /**
     * 获取68分类一级
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryDeviceType",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceType(HttpServletRequest request, HttpServletResponse response){
        try {
            ReturnResultUtil rru = baseService.queryDeviceType();
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


    /**
     * 获取68分类二级
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="queryDeviceTypeSub",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypeSub(HttpServletRequest request, HttpServletResponse response){
        try {
            if(StringUtil.isEmpty(request.getParameter("parentId"))){
                return  new ReturnResultUtil(500);
            }
            Long parentId = Long.parseLong(request.getParameter("parentId"));
            ReturnResultUtil rru = baseService.queryDeviceTypeSub(parentId);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    @RequestMapping(value="queryFileType",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryFileType(HttpServletRequest request, HttpServletResponse response){
        try {
            if (StringUtil.isEmpty(request.getParameter("fileType"))) {
                return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
            }
            String fileType = request.getParameter("fileType");
            ReturnResultUtil rru = baseService.queryFileType(fileType);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 根据设备ID查询维修需要的厂商信息
     * @param request
     * @param response
     * @param deviceId
     * @return
     */
    @RequestMapping(value="queryManufacturerByDeviceId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryManufacturerByDeviceId(HttpServletRequest request, HttpServletResponse response,Long deviceId){
        try {
            if (deviceId == null) {
                return new ReturnResultUtil(500, MessageUtil.REPAIRIDISNULL);
            }
            return baseService.queryManufacturerByDeviceId(deviceId);
        }catch (Exception e){
            e.printStackTrace();
            return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
    }

    /**
     * 根据厂家ID查询维修商信息
     * @param request
     * @param response
     * @param manufacturerId
     * @return
     */
    @RequestMapping(value="getListByManufacturerId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil getListByManufacturerId(HttpServletRequest request, HttpServletResponse response,Long manufacturerId){
        try {
            if (manufacturerId == null) {
                return new ReturnResultUtil(500, MessageUtil.MANUFACTURERIDISNULL);
            }
            return baseService.getListByManufacturerId(manufacturerId);
        }catch (Exception e){
            e.printStackTrace();
            return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
    }


    /**
     * 根据厂家ID查询维修商信息
     * @param request
     * @param response
     * @param manufacturerId
     * @return
     */
    @RequestMapping(value="/queryMinListByMamuId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMinListByMamuId(HttpServletRequest request, HttpServletResponse response,Long manufacturerId){
        try {
            if (manufacturerId == null) {
                return new ReturnResultUtil(500, MessageUtil.MANUFACTURERIDISNULL);
            }
            return baseService.queryMinListByMamuId(manufacturerId);
        }catch (Exception e){
            e.printStackTrace();
            return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
    }



    /**
     * 查询备件列表
     * @return
     */
    @RequestMapping(value="queryPartList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPartList(HttpServletRequest request, HttpServletResponse response, PartView partView){
        try{

            if(partView.getKeyId() == null){
                return new ReturnResultUtil(500,MessageUtil.PARTTKEYID);
            }
            if(partView.getKeyType() == null){
                return new ReturnResultUtil(500,MessageUtil.PARTKEYTYPE);
            }

            ReturnResultUtil rru = baseService.queryPartList(partView);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }


    /**
     * 查询供应商列表
     * @return
     */
    @RequestMapping(value="querySupplier",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil querySupplier(HttpServletRequest request, HttpServletResponse response){
        try{
            Map map = new HashMap();
               if(!StringUtil.isEmpty(request.getParameter("provinceId"))){
                   map.put("provinceId",Long.parseLong(request.getParameter("provinceId")));
               }

                if(!StringUtil.isEmpty(request.getParameter("cityId"))){
                    map.put("cityId",Long.parseLong(request.getParameter("cityId")));
                }
                if(!StringUtil.isEmpty(request.getParameter("areaId"))){
                    map.put("areaId",Long.parseLong(request.getParameter("areaId")));
                }
            ReturnResultUtil rru = baseService.querySupplier(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 查询卫计委列表
     * @return
     */
    @RequestMapping(value="queryCommission",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryCommission(HttpServletRequest request, HttpServletResponse response){
        try{
            Map map = new HashMap();
            if(!StringUtil.isEmpty(request.getParameter("provinceId"))){
                map.put("provinceId",Long.parseLong(request.getParameter("provinceId")));
            }
            if(!StringUtil.isEmpty(request.getParameter("cityId"))){
                map.put("cityId",Long.parseLong(request.getParameter("cityId")));
            }
            if(!StringUtil.isEmpty(request.getParameter("areaId"))){
                map.put("areaId",Long.parseLong(request.getParameter("areaId")));
            }
              

            ReturnResultUtil rru = baseService.queryCommission(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 根据医院ID、维修商名称查询维修商信息
     * @return
     *  2901  维修    2902	保养
     */
    @RequestMapping(value="queryHospitalMaintainer",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalMaintainer(HttpServletRequest request, HttpServletResponse response, ParameterView parameter){
        try {
            if (parameter.getHospitalId() == null) {
                return new ReturnResultUtil(500, MessageUtil.HOSPITALID);
            }
            ReturnResultUtil rru = baseService.getMaintainerByHospitalId(parameter);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
            return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
    }

    /**
     * 根据维修ID、指派方ID查询维修商信息
     * @return
     *    2901  维修    2902	保养  2903安装
     */
    @RequestMapping(value="queryByMaintainerName",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryByMaintainerName(HttpServletRequest request, HttpServletResponse response, AppointView appoint){
        try {
            if (appoint.getInstructId() == null) {
                return new ReturnResultUtil(500, MessageUtil.SERVICEID);
            }
            if (appoint.getKeyType() == null) {
                return new ReturnResultUtil(500, MessageUtil.KEYTYPEISNULL);
            }
            ReturnResultUtil rru = baseService.queryByRepairId(appoint);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
            return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        }
    }

    /**
     * 根据关键字模糊查询维修商，自动补全
     * @Create user jacky
     * @Create date 2018/3/22
     * @return
     */
    @RequestMapping(value="queryMaintainerByCompletion",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMaintainerByCompletion(HttpServletRequest request, HttpServletResponse response){
        try{
            if(StringUtil.isEmpty(request.getParameter("keyword"))){
                return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS);
            }
            String keyword = request.getParameter("keyword");
            Map map = new HashMap();
            map.put("keyword",keyword);
            ReturnResultUtil rru = baseService.queryMaintainerByCompletion(map);
            logger.debug(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 供应商获取厂家
     * @return
     */
    @RequestMapping(value="/queryManufacturerBySupplierId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryManufacturerBySupplierId(SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = sendParameterView.getUserId();
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long supplierId = sessionUser.getSupplierId();
            ManufacturerSupplierView manufacturerSupplierView = SupplierManufacturerInit.supplierManufacturerMap.get(supplierId);
            List<Manufacturer> manufacturers = manufacturerSupplierView.getManufacturers();
            resultUtil = new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,manufacturers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    /**
     * 厂家获取供应商
     * @return
     */
    @RequestMapping(value="/querySupplierByManufacturerId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil querySupplierByManufacturerId(SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = sendParameterView.getUserId();
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            ManufacturerSupplierView manufacturerSupplierView = ManufacturerSupplierInit.manufacturerSupplierMap.get(manufacturerId);
            List<Supplier> suppliers = manufacturerSupplierView.getSuppliers();
            resultUtil = new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,suppliers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    /**
     * 厂家获取维修商
     * @return
     */
    @RequestMapping(value="/queryMaintainerByManufacturerId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryMaintainerByManufacturerId(SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = sendParameterView.getUserId();
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            ManufacturerMaintainerView manufacturerMaintainerView = ManufacturerInit.manufacturerMaintainerMap.get(manufacturerId);
            List<MaintainerView> maintainerViews = manufacturerMaintainerView.getMaintainerViews();
            resultUtil = new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,maintainerViews);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    /**
     * 维修商获取厂商
     * @return
     */
    @RequestMapping(value="/queryManufacturerByMaintainerId",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryManufacturerByMaintainerId(SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = sendParameterView.getUserId();
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long maintainerId = sessionUser.getMaintainerId();
            ManufacturerMaintainerView manufacturerMaintainerView = MaintainerManufacturerInit.maintainerManufacturerMap.get(maintainerId);
            List<ManufacturerView> manufacturerViews = manufacturerMaintainerView.getManufacturerViews();
            resultUtil = new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,manufacturerViews);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    /**
     * 指派工程师手机号码和工程师i名称数据补全
     * @param request
     * @return
     */
    @RequestMapping(value="/phoneCompletion",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil phoneCompletion(HttpServletRequest request){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try {
            String serviceId = request.getParameter("serviceId");
            String engineerName = request.getParameter("engineerName");
            if(StringUtil.isEmpty(serviceId)){
                return new ReturnResultUtil(500,MessageUtil.SERVICEID);
            }
            if(StringUtil.isEmpty(engineerName)){
                return new ReturnResultUtil(500,MessageUtil.ENGINEERINFOISNULL);
            }
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("serviceId",Long.parseLong(serviceId));
            map.put("engineerName",engineerName);
            resultUtil = baseService.phoneCompletion(map);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return resultUtil;
    }
}
